﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Linq;
using OfficeInterop = Microsoft.Office.Interop;

namespace PDFConverterTest.Convertors
{
    public class PDFConvertorFactory
    {
        #region static Methods

        public static object GetOpenFileFormat<T>(string fileExtension)
            where T : IPDFConvertor
        {
            if (typeof(WordPDFConvertor) == typeof(T))
            { 
                switch(fileExtension)
                {
                    case ".txt":
                        return OfficeInterop.Word.WdOpenFormat.wdOpenFormatText;
                    case ".doc":
                        return OfficeInterop.Word.WdOpenFormat.wdOpenFormatDocument;
                    case ".docx":
                        return OfficeInterop.Word.WdOpenFormat.wdOpenFormatDocument;
                    case ".rtf":
                        return OfficeInterop.Word.WdOpenFormat.wdOpenFormatRTF;
                    default:
                        return Type.Missing;
                }
            }
            if (typeof(ExcelPDFConvertor) == typeof(T))
            {
                switch (fileExtension)
                {
                    case ".xls":
                        return OfficeInterop.Excel.XlFileFormat.xlWorkbookDefault;
                    case ".xlsx":
                        return OfficeInterop.Excel.XlFileFormat.xlWorkbookDefault;
                    default:
                        return Type.Missing;
                }
            }

            return Type.Missing;
        }

        public static object GetSaveFileFormat<T>(string fileExtension)
            where T:IPDFConvertor
        {
            if (typeof(WordPDFConvertor) == typeof(T))
            {
                return OfficeInterop.Word.WdSaveFormat.wdFormatPDF;
            }
            if (typeof(ExcelPDFConvertor) == typeof(T))
            {
                return OfficeInterop.Excel.XlFileFormat.xlTextPrinter;
            }

            return Type.Missing;
        }

        public static IPDFConvertor GetPDFConvertor(string fileExtension)
        {
            string[] wordExtension = new string[] { ".txt", ".doc", ".docx", ".rtf" };
            string[] excelExtension = new string[] { ".xls", ".xlsx" };
            if (wordExtension.Contains(fileExtension.ToLower()))
            {
                return new WordPDFConvertor();
            }
            if(excelExtension.Contains(fileExtension.ToLower()))
            {
                return new ExcelPDFConvertor();
            } 
            return new WordPDFConvertor();
        }

        #endregion

        public static string GetFileExtension(string fileName)
        {
            string fileExtension = string.Empty;
            int index = fileName.LastIndexOf(".");
            if (index > -1)
            {
                fileExtension = fileName.Substring(index, fileName.Length - index);
            }
            return fileExtension;
        }
    }
}